from base.base import Base
from selenium.webdriver.common.by import By
from time import sleep


class ProblemPage(Base):
    # 页面元素定位
    __menu_mall = (By.XPATH, '//span[text()="商场管理"]')  # 商场管理菜单
    __menu_problem = (By.XPATH, '//span[text()="通用问题"]')  # 通用问题子菜单
    __btn_edit = (By.XPATH, '//table[@class="el-table__body"]/tbody/tr[1]//button/span[contains(text(), "编辑")]')  # 第一个问题的编辑按钮
    __input_question = (By.XPATH, '//label[contains(text(), "问题")]/following-sibling::div//input')  # 问题输入框
    __input_reply = (By.XPATH, '//label[contains(text(), "回复")]/following-sibling::div//textarea') # 回复输入框
    __btn_confirm = (By.CSS_SELECTOR, '#app > div > div.main-container > section > div > div.el-dialog__wrapper > div > div.el-dialog__footer > div > button.el-button.el-button--primary.el-button--mini')  # 确认按钮
    __text_first_question = (By.XPATH, '//table[@class="el-table__body"]/tbody/tr[1]/td[2]/div')  # 第一个问题内容（断言用）

    # 页面业务方法
    def edit_problem(self, new_question: str, new_reply: str):
        """编辑通用问题业务流程"""
        # 1. 点击商场管理菜单
        self.loc_wait_ele(self.__menu_mall).click()
        sleep(3)
        # 2. 点击通用问题子菜单
        self.loc_wait_ele(self.__menu_problem).click()
        sleep(3)
        # 3. 点击第一个问题的编辑按钮
        self.loc_wait_ele(self.__btn_edit).click()
        sleep(3)
        # 4. 清空并输入新的问题和回复
        question_ele = self.loc_wait_ele(self.__input_question)
        question_ele.clear()
        question_ele.send_keys(new_question)

        reply_ele = self.loc_wait_ele(self.__input_reply)
        reply_ele.clear()
        reply_ele.send_keys(new_reply)
        # 5. 点击确认
        self.loc_wait_ele(self.__btn_confirm).click()
        sleep(3)

    def get_first_problem_content(self) -> str:
        """获取列表中第一个问题的内容（用于断言）"""
        return self.loc_wait_ele(self.__text_first_question).text